import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_yn_goods/config/app_color.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/generated/assets.dart';
import 'package:get/get.dart';

enum EnumEmptyType {
  big, //图片160
  middle, //图片120
  small //图片80
}

class EmptyView extends StatelessWidget {
  const EmptyView({
    Key? key,
    this.image,
    this.message,
    this.type = EnumEmptyType.big,
    this.textColor,
    this.mainTitle,
    this.topPadding = 0,
  }) : super(key: key);
  final double topPadding;
  final String? image;
  final String? message;
  final EnumEmptyType type;
  final Color? textColor;
  final String? mainTitle;

  @override
  Widget build(BuildContext context) {
    double imageSize = 0.0;
    double vertical = 0.0;
    switch (type) {
      case EnumEmptyType.middle:
        {
          imageSize = 120.w;
          vertical = 40.w;
        }
      case EnumEmptyType.small:
        {
          imageSize = 80.w;
          vertical = 12.w;
        }
      default:
        {
          // vertical = 160.w;
          imageSize = 160.w;
        }
    }
    return LayoutBuilder(builder: (context, constraints) {
      var delta = (constraints.maxHeight - imageSize - (vertical * 2)) / 2.0;
      var offset = (topPadding > 0 && delta > topPadding)
          ? topPadding
          : min(120, delta > 0 ? delta : 0);
      return SingleChildScrollView(
        physics: const NeverScrollableScrollPhysics(),
        child: Container(
          padding: EdgeInsets.symmetric(vertical: vertical),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              offset.verticalSpaceFromWidth,
              SizedBox(
                width: imageSize,
                height: imageSize,
                child: Image.asset(
                  image ?? Assets.commonEmptyContent,
                  fit: BoxFit.cover,
                ),
              ),
              Visibility(
                  visible: (mainTitle ?? '').isNotEmpty,
                  child: Text(
                    mainTitle ?? '',
                    style: TextStyle(
                        color: AppColor.color282828,
                        fontSize: 16.sp,
                        fontWeight: FontWeight.bold),
                  ).marginSymmetric(vertical: 8.w)),
              Text(
                message ?? LocaleKeys.no_data.tr,
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: textColor ?? Colors.black.withOpacity(0.5),
                  fontSize: 14,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ],
          ),
        ),
      );
    });
  }

  factory EmptyView.networkView({String? message, EnumEmptyType? type}) {
    return EmptyView(
      image: Assets.commonEmptyNotNetwork,
      type: type ?? EnumEmptyType.big,
      message: message ?? LocaleKeys.network_error.tr,
      textColor: AppColor.color282828,
    );
  }
}
